/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.dess.stateful.egine.core.impl;

import com.hyts.dess.stateful.egine.bean.StateRequestObject;
import com.hyts.dess.stateful.egine.bean.StateResponseObject;
import com.hyts.dess.stateful.egine.context.StateMachineContext;
import com.hyts.dess.stateful.egine.core.StateFlowProcessor;
import com.hyts.dess.stateful.egine.model.StateActionModule;
import com.hyts.dess.stateful.egine.model.StateFlowModule;

import java.util.Map;
import java.util.Objects;

/**
 * @project-name:dess
 * @package-name:com.hyts.dess.stateful.egine.core.impl
 * @author:LiBo/Alex
 * @create-date:2020-06-23 14:51
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description: 默认状态流转处理器操作功能实现
 */
public class DefaultStateFlowProcessor implements StateFlowProcessor {

    /**
     * 操作状态服务控制流转信息处理
     * @param request
     * @param stateFlowModule
     * @return
     */
    @Override
    public StateResponseObject flow(StateRequestObject request, StateFlowModule stateFlowModule) {
        // 操作处理服务流转状态控制信息
        Objects.requireNonNull(request,"状态请求对象不允许为空!");
        Objects.requireNonNull(stateFlowModule,"状态流转请求对象不允许为空!");
        // 获取转换好的模型，查找的时间复杂度为O(1)
        Map<String, Map<String, StateActionModule>> flowMap = StateMachineContext.getSTATE_MACHINE().getStateFlowModule(stateFlowModule);
        // 取出操作
        Map<String, StateActionModule> operatorMap = flowMap.get(request.getCurrentState());
        Objects.requireNonNull(operatorMap,"此状态还未配置操作");
        StateActionModule operator = operatorMap.get(request.getOperatorCode());
        Objects.requireNonNull(operator,"此状态下没有配置该操作");
        // 组装通知的数据
        StateResponseObject stateResponseObject = new StateResponseObject();
        stateResponseObject.setActionModule(operator);
        stateResponseObject.setAttach(request.getAttach());
        //执行操作
        switch (operator.getNotifyType()) {
            case "HTTP":
                //httpNotify(operator, notify);
                break;
            case "RPC":
                //throw new CommonException("暂未支持RPC通知方式");
            case "NO_NOTIFY":
                break;
            default:
                //httpNotify(operator, notify);
        }
        return stateResponseObject;
    }
}
